﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace Master
{
    class TwoWayTrafficLightStrategy : iTrafficLightStrategy
    {
        private Thread strategyThread;
        private Lane[] lanes;

        public void start(Dictionary<Side, List<Road>> roadDictionary)
        {
            List<Lane> totalList = new List<Lane>();
            foreach (List<Road> sublist in roadDictionary.Values)
                foreach (Road road in sublist)
                    foreach (Lane lane in road.getLanes())
                        totalList.Add(lane);
            lanes = totalList.ToArray();
            strategyThread = new Thread(handleTraffic);
            strategyThread.Start();

        }

        public void stop()
        {
            strategyThread.Abort();
        }

        private void handleTraffic()
        {
            try
            {
                Lane[] trafficLightLanes;
                //= new Lane[4];
                int laneCount = 0;

                foreach (Lane lane in lanes)
                {
                    if (lane.hasTrafficLight() == true)
                    {
                        //trafficLightLanes[laneCount] = lane;
                        laneCount++;
                    }
                }
                trafficLightLanes = new Lane[laneCount];
                int i = 0;
                foreach (Lane lane in lanes)
                {
                    if (lane.hasTrafficLight() == true)
                    {
                        trafficLightLanes[i] = lane;
                        i++;
                    }
                }
                Boolean horizontal = true;
                while (true)
                {
                    foreach (Lane trafficLightLane in trafficLightLanes)
                    {
                        int orientation = 0;
                        trafficLightLane.trafficLight.Dispatcher.Invoke(
                               new Action(delegate()
                        {
                            orientation = trafficLightLane.Orientation;
                        }));

                        if (horizontal == true)
                        {
                            if (orientation == 0 || orientation == 180)
                            {
                                trafficLightLane.trafficLight.Dispatcher.Invoke(
                                new Action(delegate() { trafficLightLane.trafficLight.SetValue(TrafficLight.statusProperty, TrafficLightStatus.GREEN); })
                                );
                            }
                        }
                        else
                        {
                            if (orientation == 90 || orientation == 270)
                            {
                                trafficLightLane.trafficLight.Dispatcher.Invoke(
                                new Action(delegate() { trafficLightLane.trafficLight.SetValue(TrafficLight.statusProperty, TrafficLightStatus.GREEN); })
                                );
                            }
                        }
                    }
                    Thread.Sleep(2000);
                    foreach (Lane trafficLightLane in trafficLightLanes)
                    {
                        trafficLightLane.trafficLight.Dispatcher.Invoke(
                            new Action(delegate() { trafficLightLane.trafficLight.SetValue(TrafficLight.statusProperty, TrafficLightStatus.RED); })
                        );
                    }
                    Thread.Sleep(2000);
                    if (horizontal == true) horizontal = false;
                    else horizontal = true;
                }
            }
            catch (ThreadAbortException e)
            {
                Trace.WriteLine("Thread was aborted by: " + e.GetType());
                return;
            }
        }
    }

}
